package com.adtech.farmProducts.activities.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.LinearLayout;

import com.adtech.common.Common;
import com.adtech.common.http.AsyncHttpPostFormData;
import com.adtech.common.http.AsyncHttpResponse;
import com.adtech.common.http.AsyncOkHttpClient;
import com.adtech.common.http.AsyncResponseHandler;
import com.adtech.common.model.RspModel;
import com.adtech.common.utils.AccountManagers;
import com.adtech.common.utils.MobileUtil;
import com.adtech.farmProducts.App;
import com.adtech.farmProducts.R;
import com.adtech.farmProducts.base.BaseActivity;
import com.adtech.farmProducts.base.BaseNfcActivity;
import com.adtech.farmProducts.model.FUserInfo;
import com.adtech.farmProducts.model.UserInfo;
import com.adtech.farmProducts.widget.ClearTextIconEditText;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gyf.barlibrary.ImmersionBar;

import java.io.IOException;
import java.lang.reflect.Type;

import butterknife.BindView;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

/**
 * Author:  xiaocz.
 * Descr:   员工-注册
 */
public class UserRegisterActivity extends BaseNfcActivity {


    /**
     * UI
     */
    @BindView(R.id.root_layout)
    LinearLayout rootLayout;
    @BindView(R.id.ed_name)
    ClearTextIconEditText mPhoneView;
    @BindView(R.id.ed_user_name)
    ClearTextIconEditText ed_user_name;
    @BindView(R.id.ed_pwd)
    ClearTextIconEditText edPwd;
    @BindView(R.id.ed_confirm_pwd)
    ClearTextIconEditText edConfirmPwd;
    @BindView(R.id.btn_register)
    Button btnRegister;


    /**
     * 显示注册界面
     */
    public static void show(Context context) {
        Intent intent = new Intent(context, UserRegisterActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .statusBarColor(R.color.trans).init();
    }

    @Override
    protected int getLayoutSourceId() {
        return R.layout.activity_user_register;
    }

    @Override
    protected void initWidgets() {
        //addLayoutListener(rootLayout, gs_address);
        super.initWidgets();
    }

    @OnClick({ R.id.btn_register})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_register:
                String mobile = String.valueOf(mPhoneView.getText()).trim();
                // 校验数据
                if (!MobileUtil.checkMobile(mobile)) {
                    App.getInstance().showMessage("请输入正确的手机号！");
                    return;
                }
                String code = String.valueOf(ed_user_name.getText()).trim();
                if ("".equals(code)) {
                    App.getInstance().showMessage("请输入用户名！");
                    return;
                }
                String pwd = String.valueOf(edPwd.getText()).trim();
                String confirmPwd = String.valueOf(edConfirmPwd.getText()).trim();
                if ("".equals(pwd) || pwd.length() < 6) {
                    App.getInstance().showMessage("密码长度必须大于六位数！");
                    return;
                }
                if (!pwd.equals(confirmPwd)) {
                    App.getInstance().showMessage("两次密码输入不相同！");
                    return;
                }

                FUserInfo info = new Gson().fromJson(AccountManagers.getAccount(), FUserInfo.class);
                if(info!=null){
                    onRegister(mobile, code, confirmPwd,info.getInfo().getCompanyId());
                }
                break;
        }
    }

    @OnCheckedChanged({R.id.cb_clause})
    public void OnCheckedChangeListener(CompoundButton view, boolean isCheck) {
        switch (view.getId()) {
            case R.id.cb_clause:
                if (isCheck) {
                    btnRegister.setBackground(ContextCompat.getDrawable(mContext, R.drawable.selector_btn_bg));
                    btnRegister.setEnabled(true);
                } else {
                    btnRegister.setBackground(ContextCompat.getDrawable(mContext, R.drawable.selector_btn_off_bg));
                    btnRegister.setEnabled(false);
                }
                break;
        }
    }

    /**
     * 注册员工
     */
    private void onRegister(final String mobile, String userName, String pwd,String companyId) {
        loadingDialogShow(false);
        AsyncOkHttpClient okHttpClient = new AsyncOkHttpClient();
        AsyncHttpPostFormData asyncHttpPostFormData = new AsyncHttpPostFormData();
        asyncHttpPostFormData.addFormData("mobile", mobile);
        asyncHttpPostFormData.addFormData("companyId", companyId);
        asyncHttpPostFormData.addFormData("userName", userName);
        asyncHttpPostFormData.addFormData("password", pwd);
        okHttpClient.posts(Common.Constant.API_IP + "user/addEmployee", asyncHttpPostFormData, new AsyncResponseHandler() {
            @Override
            public void onFailure(IOException e) {
                e.printStackTrace();
                onDismiss();
            }

            @Override
            public void onSuccess(AsyncHttpResponse response) {
                onDismiss();
                Type type = new TypeToken<RspModel<FUserInfo>>() {
                }.getType();
                RspModel<FUserInfo> rspModel = getAnalysis(response, type, "注册员工");
                if (rspModel == null) {
                    return;
                }
                switch (rspModel.getCode()) {
                    case 1:
                        if (rspModel.getData()!=null) {
                            App.getInstance().showMessage("添加员工成功！");
                            setResult(200);
                            finish();
                        }else {
                            App.getInstance().showMessage(rspModel.getMessage());
                        }
                        break;
                    default:
                        App.getInstance().showMessage(rspModel.getMessage());
                        break;
                }
            }
        });
    }

    /**
     * 实现短信验证码倒计时功能
     */

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
